package net.omnivention.wulumuqi.html;

import java.util.HashMap;

import org.jdom.Element;
import org.jdom.Namespace;

public class HtmlElementFactory {
	private final static HashMap<String, Class> elementClassMap = new HashMap<String, Class>();
	static {
		elementClassMap.put("a", A.class);
		elementClassMap.put("h1", H1.class);
		elementClassMap.put("input", Input.class);
		elementClassMap.put("span", Span.class);
		elementClassMap.put("table", Table.class);
		elementClassMap.put("tr", Tr.class);
	}

	public final static Namespace htmlns = Namespace.getNamespace("", "http://www.w3.org/1999/xhtml");

	public static Element createElement(String elementName, Namespace ns) {
		Class clz = elementClassMap.get(elementName);
		if (clz != null) {
			Element element;
			try {
				element = (Element) clz.newInstance();
				element.setName(elementName);
				element.setNamespace(ns);

				return element;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	public static Element createElement(String elementName) {
		Element element = null;
		Class clz = elementClassMap.get(elementName);
		if (clz != null) {
			try {
				element = (Element) clz.newInstance();
				element.setName(elementName);
			} catch (Exception e) {
			}
		}
		if (element == null) {
			element = new Element(elementName);
		}

		element.setNamespace(htmlns);

		return element;
	}
}
